/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.serializer.decomposer;

import at.schauer.gregor.vienna.context.ViennaContext;
import at.schauer.gregor.vienna.serializer.filter.AbstractFieldFilter;
import at.schauer.gregor.vienna.serializer.filter.FilterSupport;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gregor Schauer
 */
public class FilterDecomposer<T> extends FilterSupport<T> implements ObjectDecomposer<T> {
	protected ViennaContext ctx;

	public FilterDecomposer(ViennaContext ctx) {
		this(ctx, new ArrayList<AbstractFieldFilter<?>>());
	}

	public FilterDecomposer(ViennaContext ctx, List<AbstractFieldFilter<?>> filters) {
		super(filters);
		this.ctx = ctx;
	}

	@Override
	public Map<String, Object> deconstruct(final Map<String, Object> map, final T obj) {
		ReflectionUtils.doWithFields(obj.getClass(), new ReflectionUtils.FieldCallback() {
			@Override
			public void doWith(Field field) throws IllegalAccessException {
				map.put(field.getName(), FieldUtils.readField(field, obj, true));
			}
		}, this);
		return map;
	}
}
